<?php
/*
*description:订单统计
*author:李飞@天众科技
*time:2017/05/04
*/
namespace Admin\Controller;
use Think\Controller;
use Think\Model;
class OrderapiController extends Controller {
	//单个订单信息
	public function order_info(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/goods_info.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$id=$param['id'];
		$order_no=$param['order_no'];
		if(!empty($id)){
			$map['id']=$param['id'];
		}
		if(!empty($order_no)){
			$map['order_no']=$param['order_no'];
		}
		$this->db_config($warehouse_code);
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$info=$m->where($map)->find();
		$sql=$m->getlastsql();
		file_put_contents('./log/order_info.log',date('Y-m-d H:i:s').var_export($sql,true).PHP_EOL,FILE_APPEND);//日志导出sql
		if($info){
			$return['status']=1;
			$return['info']=$info;
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']='没有数据';
			$this->ajaxReturn($return);
		}
	}

	//订单统计
    public function order(){
		$m=M('Warehouse');
		//验证用户名/密码--begin
		// $username=I('username');
		// $password=I('password');
		// if(){
			
		// }
		// md5($username.$password)
		//验证用户名/密码--end
		$Model = new Model();
		$count=0;

		$datas=file_get_contents('php://input');
		$param=json_decode($datas,true);
		file_put_contents('./log/order.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		
		$warehouse_code=$param['warehouse_code'];//仓库编码
		// dump($warehouse_code);
		// die;
		if(empty($warehouse_code)){
			$return['status']=0;
			$return['info']="没有仓库编码";
			$this->ajaxReturn($return);
		}else{
			// $map['status']=1;
			$map['warehouse_code']=array('in',$warehouse_code);
			$warehouse_list=$m->where($map)->select();
		}
		
		$info_count=$param['info_count'];//汇总数据参数
		if(empty($info_count)){
			$info_count=0;
		}
		// echo $m->getlastsql();
		$wheres="where ";
		$where=" 1 ";
		
		//时间查询-begin
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time)) {
			$start_time=strtotime($start_time);
			$where.="and add_time > '$start_time' ";
        }
        if(!empty($end_time)) {
			$end_time=strtotime($end_time);
			$where.="and add_time < '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
			$where.="and (add_time > '$start_time' and add_time < '$end_time') ";
        }
		//时间查询-end
		$erp_code=$param['erp_code'];//企业编码
		if(!empty($erp_code)){
			$where.="and erp_code='$erp_code' ";
		}
		
		$goods_no=$param['goods_no'];//商品编码
		if(!empty($goods_no)){
			$where.="and goods_no like '%$goods_no%' ";
		}
		
		$goods_name=$param['goods_name'];//商品名称
		if(!empty($goods_name)){
			$where.="and goods_name like '%$goods_name%' ";
		}
		
		$listrows=$param['listrows'];//每页显示条数
		if(!empty($listrows)){
			$listrows=$listrows;
		}else{
			$listrows=20;
		}
		
		$p=$param['p'];//页数
		if(!empty($p)){
			$p=$p;
		}else{
			$p=1;
		}
		$limit="limit ".($p-1)*$listrows.",".$listrows;

		foreach($warehouse_list as $k=>$v){
			$this->db_config($v['warehouse_code']);
			$p=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
			$count_s=$p->where($where)->count();
			$count=$count+$count_s;//总条数
			if($k==0){
				if($info_count){//汇总数据
					$sql="select * from (select *,sum('receive_amount') a_receive_amount,sum('freez_amount') a_freez_amount,sum('storage_amount') a_storage_amount from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."pushorder ".$wheres.$where."group by goods_no,manual_id ";
				}else{//详细数据
					$sql="select * from (select *,".$v['warehouse_code']." warehouse_code from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."pushorder ".$wheres.$where;
				}
			}else{
				if($info_count){
					$sql.=" union all select *,sum('receive_amount') a_receive_amount,sum('freez_amount') a_freez_amount,sum('storage_amount') a_storage_amount from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."pushorder ".$wheres.$where."group by goods_no,manual_id ";
				}else{
					$sql.=" union all select *,".$v['warehouse_code']." warehouse_code from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."pushorder ".$wheres.$where;
				}
			}
		}
		$sql.=") t ".$limit;
		// echo $sql;
		// die;
		file_put_contents('./log/order.log',date('Y-m-d H:i:s').var_export($sql,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$list=$Model->query($sql);
		if(empty($list)){
			$return['status']=0;
			$return['info']="没有数据！";
			$this->ajaxReturn($return);
		}
		$return['status']=1;
		$return['info']['count']=$count;
		$return['info']['list']=$list;
		$this->ajaxReturn($return);
	}

	/*
	 *获取数据库配置
     */
    public function db_config($warehouse_code){
		
		$info=M('Warehouse')->where(array('warehouse_code'=>$warehouse_code))->find();//获取配置表中对应域名的id
		$ss=M('Warehouse')->getlastsql();
		file_put_contents('./log/db_config.log',date('Y-m-d H:i:s').var_export($ss,true).PHP_EOL,FILE_APPEND);//日志导出sql
		// echo M()->getlastsql();
		
		// dump($info);
		if($info){  //天众数据库库配置
			!empty($info['db_ip'])?$db_ip=$info['db_ip']:$db_ip=C('DEPOT_DB_HOST');
			!empty($info['db_name'])?$db_name=$info['db_name']:$db_name=C('DEPOT_DB_NAME');
			!empty($info['db_username'])?$db_username=$info['db_username']:$db_username=C('DEPOT_DB_USER');
			!empty($info['db_password'])?$db_password=$info['db_password']:$db_password=C('DEPOT_DB_PWD');
			!empty($info['db_port'])?$db_port=$info['db_port']:$db_port=C('DEPOT_DB_PORT');
			!empty($info['db_pre'])?$db_pre=$info['db_pre']:$db_pre=C('DEPOT_DB_PREFIX');
			
			//当前仓库数据库配置
			C('DB_DEPOT.DB_TYPE','mysql');
			C('DB_DEPOT.DB_HOST',$db_ip);
			C('DB_DEPOT.DB_NAME',$db_name);
			C('DB_DEPOT.DB_USER',$db_username);
			C('DB_DEPOT.DB_PWD',$db_password);
			C('DB_DEPOT.DB_PORT',$db_port);
			C('DB_PRS',$db_pre);
			C('DB_DEPOT.DB_CHARSET','utf8');
		}
		
		//加载自定义配置信息
		$m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
		$list=$m->where(array('status'=>1))->getField('en_name,value');
		C('base_config',$list);
    }
}